package cn.wolfcode.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.wolfcode.business.domain.Statement;
import cn.wolfcode.business.mapper.StatementMapper;
import cn.wolfcode.business.vo.StatementItemVO;
import cn.wolfcode.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.StatementItemMapper;
import cn.wolfcode.business.domain.StatementItem;
import cn.wolfcode.business.service.IStatementItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算单明细Service业务层处理
 * 
 * @author dafei
 * @date 2023-10-31
 */
@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService 
{
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementMapper statementMapper;

    /**
     * 查询结算单明细
     * 
     * @param id 结算单明细主键
     * @return 结算单明细
     */
    @Override
    public StatementItem selectStatementItemById(Long id)
    {
        return statementItemMapper.selectStatementItemById(id);
    }

    /**
     * 查询结算单明细列表
     * 
     * @param statementItem 结算单明细
     * @return 结算单明细
     */
    @Override
    public List<StatementItem> selectStatementItemList(StatementItem statementItem)
    {
        return statementItemMapper.selectStatementItemList(statementItem);
    }

    /**
     * 新增结算单明细
     * 
     * @param statementItem 结算单明细
     * @return 结果
     */
    @Override
    public int insertStatementItem(StatementItem statementItem)
    {
        return statementItemMapper.insertStatementItem(statementItem);
    }

    /**
     * 修改结算单明细
     * 
     * @param statementItem 结算单明细
     * @return 结果
     */
    @Override
    public int updateStatementItem(StatementItem statementItem)
    {
        return statementItemMapper.updateStatementItem(statementItem);
    }

    /**
     * 批量删除结算单明细
     * 
     * @param ids 需要删除的结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteStatementItemByIds(Long[] ids)
    {
        return statementItemMapper.deleteStatementItemByIds(ids);
    }

    /**
     * 删除结算单明细信息
     * 
     * @param id 结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteStatementItemById(Long id)
    {
        return statementItemMapper.deleteStatementItemById(id);
    }

    //@Transactional
    @Override
    public void saveItem(StatementItemVO vo) {
        //参数判断---
        if(vo  == null){
            throw new ServiceException("参数异常");
        }
        if(vo.getStatementId()  == null){
            throw new ServiceException("参数异常");
        }
        //结算单状态必须消费中
        Statement statement = statementMapper.selectStatementById(vo.getStatementId());

        if(statement == null || !Statement.STATUS_CONSUME.equals(statement.getStatus())){
            throw new ServiceException("只有消费中才允许操作");
        }
        //删除结算单明细需要结算单id, 如果是清空(结算单列表为空的)操作,之前vo是无法
        //传入结算单id, 需要前端额外添加
        statementItemMapper.deleteByStatementId(vo.getStatementId());

        List<StatementItem> items = vo.getStatementItems();
//        if(items.size() == 0){
//            //更新结算单: 总金额, 总数量  优化价格
//            statementMapper.updateAmount(vo.getStatementId(), BigDecimal.ZERO, 0L, BigDecimal.ZERO);
//            return;
//        }
        Long statementId = vo.getStatementId();  //获取结算单id
        //当进行保存前, 需要情况当前结算单下的所有结算明细
        BigDecimal totalAmount = BigDecimal.ZERO;   // 总金额 : 0
        Long totalQuantity = 0L;   // 总数量: 0

        for (StatementItem item : items) {
            //保存结算单明细
            statementItemMapper.insertStatementItem(item);
            //总数量
            totalQuantity += item.getItemQuantity();
            //总金额 += 数量 * 明细单价
            //item.getItemQuantity() * item.getItemPrice()
            totalAmount = totalAmount.add(item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity())));
        }
        //价格判断--优惠价 小于 总金额
        if(vo.getDiscountPrice().compareTo(totalAmount) > 0){
            throw new RuntimeException("优化价格不能大于总价格");
        }
        //更新结算单: 总金额, 总数量  优化价格
        statementMapper.updateAmount(statementId, totalAmount, totalQuantity, vo.getDiscountPrice());
    }
}
